Skip to content

fix(opencode): route session.created through event dispatcher (421)#442

Open
ousamabenyounes wants to merge 1 commit into
JuliusBrussee:mainfrom
ousamabenyounes:fix/issue-421
Open

fix(opencode): route session.created through event dispatcher (421)#442
ousamabenyounes wants to merge 1 commit into
JuliusBrussee:mainfrom
ousamabenyounes:fix/issue-421

Conversation

@ousamabenyounes

@ousamabenyounes ousamabenyounes commented May 26, 2026

Copy link
Copy Markdown

Summary

Fixes #421 — opencode plugin's session.created handler never fires on opencode >= 1.15.

opencode >= 1.15 dispatches session/lifecycle events through a single event handler that switches on event.type (plugin docs). The previous direct top-level 'session.created' key was silently ignored, so the .caveman-active flag was never written on session start and the per-prompt reinforcement in tui.prompt.append bailed every turn (it reads the flag and returns when empty). Always-on caveman in opencode kept working only via the separately-installed ~/.config/opencode/AGENTS.md; the plugin's dynamic-state layer was silently inert.

tui.prompt.append is unchanged — TUI events still use the direct-key pattern.

Fix

  • Extract handleSessionCreated (single source for session-start logic, reusable from any dispatcher).
  • Replace the direct 'session.created' key with an event handler that filters on event.type === 'session.created'.

TDD verification

  • RED check (tests on main without prod fix): 2/2 new tests fail
    • event handler missingtypeof handlers.event === 'function' asserts undefined
    • handlers.event is not a function — TypeError on the new event-dispatch test
  • GREEN check (tests with prod fix): 2/2 new tests pass

Verification

  • Baseline tests on main: 51 total, 47 pass, 4 pre-existing failures (installer command-file copy, unrelated)
  • Post-fix tests: 52 total, 48 pass, same 4 pre-existing failures (no green→non-green flips, no new failures)
  • New tests: 1 added (event dispatcher ignores unrelated event types + dispatches session.created); 1 modified to use the v1.15 dispatch shape and assert the direct-key is gone

Files changed

File Change
src/plugins/opencode/plugin.js Extract handleSessionCreated; route through event dispatcher
tests/installer/opencode.test.mjs Update smoke test to v1.15 API; add event dispatcher coverage test

opencode >= 1.15 dispatches session/lifecycle events through a single
`event` handler that switches on `event.type`. The previous direct
top-level `'session.created'` key was silently ignored, so the
`.caveman-active` flag was never written on session start and the
per-prompt reinforcement in `tui.prompt.append` bailed every turn.

Extract `handleSessionCreated` and wire it under an `event` dispatcher
that filters on `event.type === 'session.created'`. `tui.prompt.append`
is unchanged — TUI events still use the direct-key pattern.

Co-Authored-By: Claude <noreply@anthropic.com>
@ousamabenyounes

Copy link
Copy Markdown
Author

Verified RED/GREEN proof (run on eab8052 against origin/main)

Test runner: node --test --test-name-pattern='opencode event handler dispatches|opencode plugin handles /caveman ultra and stop caveman via tui.prompt.append' tests/installer/opencode.test.mjs.

Procedure:

  1. GREEN — fix as committed (HEAD = eab8052)
  2. git checkout origin/main -- src/plugins/opencode/plugin.js (revert prod fix, keep new tests)
  3. RED — same command, captured below
  4. git checkout HEAD -- src/plugins/opencode/plugin.js (restore)

GREEN (with fix)

# Subtest: opencode plugin handles /caveman ultra and stop caveman via tui.prompt.append
ok 1 - opencode plugin handles /caveman ultra and stop caveman via tui.prompt.append
  ---
  duration_ms: 92.969133
  type: 'test'
  ...
# Subtest: opencode event handler dispatches session.created and ignores other event types
ok 2 - opencode event handler dispatches session.created and ignores other event types
  ---
  duration_ms: 106.131722
  type: 'test'
  ...
1..2
# tests 2
# suites 0
# pass 2
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 268.942668
# pass 2
# fail 0

RED (prod fix reverted, tests unchanged)

# Subtest: opencode plugin handles /caveman ultra and stop caveman via tui.prompt.append
not ok 1 - opencode plugin handles /caveman ultra and stop caveman via tui.prompt.append
  ---
  duration_ms: 103.986678
  type: 'test'
  location: '/tmp/contribute-caveman-421/tests/installer/opencode.test.mjs:251:1'
  failureType: 'testCodeFailure'
  error: |-
    event handler missing
    + actual - expected
    
    + 'undefined'
    - 'function'
    
  code: 'ERR_ASSERTION'
  name: 'AssertionError'
  expected: 'function'
  actual: 'undefined'
  operator: 'strictEqual'
  stack: |-
    TestContext.<anonymous> (file:///tmp/contribute-caveman-421/tests/installer/opencode.test.mjs:281:12)
    async Test.run (node:internal/test_runner/test:1054:7)
    async startSubtestAfterBootstrap (node:internal/test_runner/harness:296:3)
  ...
# Subtest: opencode event handler dispatches session.created and ignores other event types
not ok 2 - opencode event handler dispatches session.created and ignores other event types
  ---
  duration_ms: 120.196507
  type: 'test'
  location: '/tmp/contribute-caveman-421/tests/installer/opencode.test.mjs:292:1'
  failureType: 'testCodeFailure'
  error: 'handlers.event is not a function'
  code: 'ERR_TEST_FAILURE'
  name: 'TypeError'
  stack: |-
    TestContext.<anonymous> (file:///tmp/contribute-caveman-421/tests/installer/opencode.test.mjs:309:20)
    async Test.run (node:internal/test_runner/test:1054:7)
    async Test.processPendingSubtests (node:internal/test_runner/test:744:7)
  ...
1..2
# tests 2
# suites 0
# pass 0
# fail 2
# cancelled 0
# skipped 0
# todo 0
# duration_ms 306.54369
# pass 0
# fail 2

Both tests fail with the exact assertion messages the test bodies expect (event handler missing and handlers.event is not a function), proving the tests catch the bug rather than asserting internal implementation.

Full captured logs persisted locally and available on request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode plugin: session.created handler never fires on opencode >= 1.15 — flag file never written

1 participant